<template>
    <div>
        <!-- Drawer抽屉 -->
        <Drawer :title="title" v-model="visible" class="c-drawer01"  draggable :mask-closable="type == '0'">
            <div :style="{ maxHeight: maxHeight }" class="drawer-content">
                <Form ref="form" :model="form" :rules="formValidate" label-position="top">
                    <Row :gutter="32">
                        <Col span="24">
                            <FormItem label="外文名称" prop="foreignName">
                                <Input v-model="form.foreignName" clearable />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row :gutter="32">
                        <Col span="24">
                            <FormItem label="中文名称" prop="chineseName">
                                <Input v-model="form.chineseName" clearable />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row :gutter="32">
                        <Col span="24">
                            <FormItem label="所在国家或地区" prop="gjOrDq">
                                <Input v-model="form.gjOrDq" clearable />
                            </FormItem>
                        </Col>
                    </Row>
                    <!--          <Row :gutter="32">-->
                    <!--            <Col span="24">-->
                    <!--              <FormItem label="排序值" prop="sortOrder" >-->
                    <!--                <InputNumber v-model="form.sortOrder" style="width:100%"></InputNumber>-->
                    <!--              </FormItem>-->
                    <!--            </Col>-->
                    <!--          </Row>-->
                </Form>
            </div>
            <div class="drawer-footer br" v-show="type != '0'">
                <Button type="primary" :loading="submitLoading" @click="submit">提交</Button>
                <Button @click="visible = false">取消</Button>
            </div>
        </Drawer>
    </div>
</template>

<script>
// 根据你的实际请求api.js位置路径修改
import { addGwschoolCode, editGwschoolCode } from '@/api/gwschoolCode.js';
export default {
    name: 'addEdit',
    components: {},
    props: {
        value: {
            type: Boolean,
            default: false
        },
        data: {
            type: Object
        },
        type: {
            type: String,
            default: '0'
        }
    },
    data() {
        return {
            visible: this.value,
            title: '',
            submitLoading: false,
            maxHeight: 510,
            form: {
                // 添加或编辑表单对象初始化数据
                foreignName: '',
                chineseName: '',
                gjOrDq: '',
                sortOrder: 0
            },
            // 表单验证规则
            formValidate: {
                foreignName: [{ required: true, message: '不能为空', trigger: 'change' }],
                chineseName: [{ required: true, message: '不能为空', trigger: 'change' }],
                gjOrDq: [{ required: true, message: '不能为空', trigger: 'change' }]
                // sortOrder: [{ type: "number", required: true, message: "不能为空", trigger: "change" }],
            }
        };
    },
    methods: {
        init() {},
        submit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    if (this.type == '1') {
                        // 编辑
                        this.submitLoading = true;
                        editGwschoolCode(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.success) {
                                this.$Message.success('操作成功');
                                this.$emit('on-submit', true);
                                this.visible = false;
                            }
                        });
                    } else {
                        // 添加
                        this.submitLoading = true;
                        addGwschoolCode(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.success) {
                                this.$Message.success('操作成功');
                                this.$emit('on-submit', true);
                                this.visible = false;
                            }
                        });
                    }
                }
            });
        },
        setCurrentValue(value) {
            if (value === this.visible) {
                return;
            }
            if (this.type == '1') {
                this.title = '编辑';
                this.maxHeight = Number(document.documentElement.clientHeight - 121) + 'px';
            } else if (this.type == '2') {
                this.title = '添加';
                this.maxHeight = Number(document.documentElement.clientHeight - 121) + 'px';
            } else {
                this.title = '详细信息';
                this.maxHeight = '100%';
            }
            // 清空数据
            this.$refs.form.resetFields();
            if (this.type == '0' || this.type == '1') {
                // 回显数据处理
                this.form = this.data;
            } else {
                // 添加
                delete this.form.id;
            }
            this.visible = value;
        }
    },
    watch: {
        value(val) {
            this.setCurrentValue(val);
        },
        visible(value) {
            this.$emit('input', value);
        }
    },
    mounted() {
        this.init();
    }
};
</script>

<style lang="less">
// 建议引入通用样式 具体路径自行修改 可删除下面样式代码
// @import "../../../styles/drawer-common.less";
.drawer-footer {
    z-index: 10;
    width: 100%;
    position: absolute;
    bottom: 0;
    left: 0;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: left;
    background: #fff;
}

.drawer-content {
    overflow: auto;
}

.drawer-content::-webkit-scrollbar {
    display: none;
}

.drawer-header {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
}

.drawer-title {
    font-size: 16px;
    color: rgba(0, 0, 0, 0.85);
    display: block;
    margin-bottom: 16px;
}
</style>
<style lang="less" scoped >
.cml5 {
  margin-left:5px
}

.c-drawer01 {
  /deep/ .ivu-drawer {
        width:500px !important;
    }
}

.cmr10 {
  margin-right: 10px
}

</style>